package com.hd.shiro2204.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hd.shiro2204.entity.Perms;
import com.hd.shiro2204.entity.Role;
import com.hd.shiro2204.entity.RolePerms;
import com.hd.shiro2204.entity.User;
import com.hd.shiro2204.service.PermsService;
import com.hd.shiro2204.service.RolePermsService;
import com.hd.shiro2204.service.RoleService;
import com.hd.shiro2204.service.UserService;
import org.apache.catalina.mapper.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    PermsService permsService;
    @Autowired
    RolePermsService rolePermsService;
    @GetMapping("/get")
    public HashMap<String,Object> get(String roleName){
        HashMap<String,Object> map=new HashMap<>();
        List<User> users = userService.getUsers(roleName);
        List<Object> list=new ArrayList<>();
        if (users!=null){
            map.put("msg", "ok");
            map.put("code", 1);
            for (User user : users) {
                HashMap<String,Object> map1=new HashMap<>();
                map1.put("id",user.getId());
                map1.put("username",user.getUsername());
               list.add(map1);
            }
            map.put("data",list);
        }else {
            map.put("msg", "接口操作失败，请联系运维人员");
            map.put("code", -1);
        }
        return map;

    }
    @GetMapping("/get1")
    public HashMap<String,Object> get1(String permsName){
        HashMap<String,Object> map=new HashMap<>();
        List<User> users = userService.getUsersByPerms(permsName);
        List<Object> list=new ArrayList<>();
        if (users!=null){
            map.put("msg", "ok");
            map.put("code", 1);
            for (User user : users) {
                HashMap<String,Object> map1=new HashMap<>();
                map1.put("id",user.getId());
                map1.put("username",user.getUsername());
                list.add(map1);
            }
            map.put("data",list);
        }else {
            map.put("msg", "接口操作失败，请联系运维人员");
            map.put("code", -1);
        }
        return map;

    }
    @GetMapping("/addRole")
    public String addRole(Role role){
        boolean save = roleService.save(role);
        String result;
        if (save==true){
            result ="添加角色成功";
        }else {
            result ="添加角色失败";
        }
        return result;

    }
    @GetMapping("/addPerms")
    public String addPerms(Perms perms){
        boolean save = permsService.save(perms);
        String result;
        if (save==true){
            result ="添加权限成功";
        }else {
            result ="添加权限失败";
        }
        return result;

    }
    @GetMapping("/addPermsByRole")
    public String addPermsByRole(String roleName, String permsName){
        Role role= roleService.getOne(new QueryWrapper<Role>().eq("name", roleName));
        //System.out.println(role);
        Perms perms = permsService.getOne(new QueryWrapper<Perms>().eq("name", permsName));
        //System.out.println(perms);
        RolePerms rolePerms = new RolePerms();
        rolePerms.setRoleId(role.getId());
        rolePerms.setPermsId(perms.getId());
        boolean save = rolePermsService.save(rolePerms);
        String result;
        if (save==true){
            result ="给角色"+roleName+"添加"+permsName+"权限成功";
        }else {
            result ="添加权限失败";
        }
        return result;

    }
}
